﻿using MySpace.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_Member_List : System.Web.UI.Page
{
    protected string type = "";
    protected static int nowpages;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.type = string.IsNullOrEmpty(Request.QueryString["type"]) ? "" : Request.QueryString["type"];
            nowpages = 1;
            DataBd();
        }
        
    }
    public void DataBd()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = string.Format("select * from member where m_type={0} order by  id desc",type);
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 20;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        Label1.Text = nowpages.ToString();
        Label2.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        if(type.Equals("2"))
        {
            GridView1.Columns[2].Visible = false;
            GridView1.Columns[3].Visible = false;
        }
        GridView1.DataSource = ps;
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sql = string.Format("delete from member where id={0}", ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text);
        DBHandler dbh = GetHandler.Init();
        try
        {
            dbh.CommandText = sql;
            dbh.ExecuteNonQuery();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除成功');</script>");
            this.DataBd();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除失败');</script>");
        }
    }
    protected void delall_Click(object sender, EventArgs e)
    {
        int i = 0;
       

        foreach (GridViewRow gv in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gv.FindControl("CheckBox1");
            Label lb = (Label)gv.FindControl("id");
            if (cb.Checked)
            {
                DBHandler dbh = GetHandler.Init();
                dbh.CommandText = string.Format("delete from member where id={0}", lb.Text);
                dbh.ExecuteNonQuery();
                i++;
            }
        }
        if (i == 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请选择要删除的信息');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('成功删除" + i + "条信息');</script>");
        }
        this.DataBd();
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        DataBd();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        DataBd();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        DataBd();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        DataBd();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(Label2.Text);
        DataBd();
    }

}